package com.test.web.test;

import cn.hutool.core.io.IoUtil;
import com.test.common.core.domain.R;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

/**
 * @author zxw
 * @date 2023/1/8 下午3:00
 */
@RequestMapping("annotationHttp")
@RestController
public class TestAnnotationHttp {

    private AtomicInteger atomicInteger = new AtomicInteger(1);

    @GetMapping("testGet")
    public R testGet(String[] testParams, HttpServletRequest request, Boolean retry) {

        // 测试重试
        if (retry != null && retry) {
            int i = atomicInteger.getAndAdd(1);
            System.out.println("i:" + i);
            if (i % 3 != 0) {
                ThreadUtil.sleep(3000);
            }
        }

        return R.success()
                .put("header", request.getHeader("testHeader"))
                .put("params", testParams);
    }

    @PostMapping("testPost")
    public R testPost(String[] testParams, @RequestBody Map map, HttpServletRequest request) {

        return R.success(map)
                .put("header", request.getHeader("testHeader"))
                .put("params", testParams)
                .put("body", map);
    }

    @PutMapping("testPut")
    public R testPut(String[] testParams, @RequestBody Map map, HttpServletRequest request) {

        return R.success(map)
                .put("header", request.getHeader("testHeader"))
                .put("params", testParams)
                .put("body", map);
    }

    @DeleteMapping("testDelete")
    public R testDelete(String deleteId, HttpServletRequest request) {

        return R.success(deleteId).put("header", request.getHeader("testHeader"));
    }

    @PostMapping("testUpload")
    public R testUpload(@RequestParam MultipartFile file, HttpServletRequest request) throws IOException {

        file.transferTo(new File("/home/manjaro/test_file/annotation_http/test.jpg"));

        return R.success().put("header", request.getHeader("test"));
    }

    @GetMapping("testDownload")
    public void testDownload(HttpServletResponse response) throws IOException {

        File file = new File("/home/manjaro/test_file/annotation_http/test.jpg");
        byte[] bytes;
        try (FileInputStream fileInputStream = new FileInputStream(file);){
            bytes = IoUtil.readBytes(fileInputStream);
        }
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"test.jpg\"");
        response.addHeader("Content-Length", "" + bytes.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(bytes, response.getOutputStream());
    }
}
